package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.constant.WxOperateType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysWxOperateRecord;
import com.ruoyi.system.domain.SysWxapplyRecord;
import com.ruoyi.system.mapper.SysWxOperateRecordMapper;
import com.ruoyi.system.mapper.SysWxapplyRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysWxBeforeGroupMapper;
import com.ruoyi.system.domain.SysWxBeforeGroup;
import com.ruoyi.system.service.ISysWxBeforeGroupService;

/**
 * wx群初步采集Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-28
 */
@Service
public class SysWxBeforeGroupServiceImpl implements ISysWxBeforeGroupService 
{
    @Autowired
    private SysWxBeforeGroupMapper sysWxBeforeGroupMapper;
    @Autowired
    private SysWxapplyRecordMapper sysWxapplyRecordMapper;
    @Autowired
    private SysWxOperateRecordMapper sysWxOperateRecordMapper;
    /**
     * 查询wx群初步采集
     * 
     * @param id wx群初步采集主键
     * @return wx群初步采集
     */
    @Override
    public SysWxBeforeGroup selectSysWxBeforeGroupById(Long id)
    {
        return sysWxBeforeGroupMapper.selectSysWxBeforeGroupById(id);
    }

    /**
     * 查询wx群初步采集列表
     * 
     * @param sysWxBeforeGroup wx群初步采集
     * @return wx群初步采集
     */
    @Override
    public List<SysWxBeforeGroup> selectSysWxBeforeGroupList(SysWxBeforeGroup sysWxBeforeGroup)
    {
        return sysWxBeforeGroupMapper.selectSysWxBeforeGroupList(sysWxBeforeGroup);
    }

    /**
     * 新增wx群初步采集
     * 
     * @param sysWxBeforeGroup wx群初步采集
     * @return 结果
     */
    @Override
    public int insertSysWxBeforeGroup(SysWxBeforeGroup sysWxBeforeGroup)
    {
        sysWxBeforeGroup.setCreateTime(DateUtils.getNowDate());
        sysWxBeforeGroup.setAttribute2(sysWxBeforeGroup.getMember());
        sysWxBeforeGroup.setAttribute5(DateUtils.getNowDate());
        int dd= sysWxBeforeGroupMapper.insertSysWxBeforeGroup(sysWxBeforeGroup);
        if (sysWxBeforeGroup.getAttribute1()!=null&&sysWxBeforeGroup.getAttribute1().trim().equals("0")){
            //加群记录改为成功
            SysWxapplyRecord sysWxapplyRecord=new SysWxapplyRecord();
            sysWxapplyRecord.setAreamemo(sysWxBeforeGroup.getAreamemo());
            sysWxapplyRecord.setGroupid(sysWxBeforeGroup.getId());
            sysWxapplyRecord.setGroupNumber(sysWxBeforeGroup.getGroupNumber());
            sysWxapplyRecord.setGroupownerName(sysWxBeforeGroup.getGroupownerName());
            sysWxapplyRecord.setGroupownerPhone(sysWxBeforeGroup.getGroupownerPhone());
            sysWxapplyRecord.setJoinmemo(sysWxBeforeGroup.getJoinmemo());
            sysWxapplyRecord.setMember(sysWxBeforeGroup.getMember());
            sysWxapplyRecord.setSubBrand(sysWxBeforeGroup.getSubBrand());
            sysWxapplyRecord.setStatus("0");
            sysWxapplyRecordMapper.insertSysWxapplyRecord(sysWxapplyRecord);
            //群状态变更记录也增加上
            SysWxOperateRecord sysWxOperateRecord=new SysWxOperateRecord();
            sysWxOperateRecord.setWxgroupid(sysWxBeforeGroup.getId());
            sysWxOperateRecord.setWxOpareteType(WxOperateType.ADD_PERSON);
            sysWxOperateRecord.setGroupNumber(sysWxBeforeGroup.getGroupNumber());
            sysWxOperateRecord.setGroupownerPhone(sysWxBeforeGroup.getGroupownerPhone());
            sysWxOperateRecord.setGroupownerName(sysWxBeforeGroup.getGroupownerName());
            sysWxOperateRecord.setRemark("添加成员,成员id为--"+sysWxBeforeGroup.getMember());
            sysWxOperateRecordMapper.insertSysWxOperateRecord(sysWxOperateRecord);

        }else {
          //插入加群记录表
            if (sysWxBeforeGroup.getMember()!=null&&!(sysWxBeforeGroup.getMember().trim().isEmpty())){
                SysWxapplyRecord sysWxapplyRecord=new SysWxapplyRecord();
                sysWxapplyRecord.setAreamemo(sysWxBeforeGroup.getAreamemo());
                sysWxapplyRecord.setGroupid(sysWxBeforeGroup.getId());
                sysWxapplyRecord.setGroupNumber(sysWxBeforeGroup.getGroupNumber());
                sysWxapplyRecord.setGroupownerName(sysWxBeforeGroup.getGroupownerName());
                sysWxapplyRecord.setGroupownerPhone(sysWxBeforeGroup.getGroupownerPhone());
                sysWxapplyRecord.setJoinmemo(sysWxBeforeGroup.getJoinmemo());
                sysWxapplyRecord.setMember(sysWxBeforeGroup.getMember());
                sysWxapplyRecord.setSubBrand(sysWxBeforeGroup.getSubBrand());
                sysWxapplyRecord.setStatus("1");
                sysWxapplyRecordMapper.insertSysWxapplyRecord(sysWxapplyRecord);
            }

        }
        return dd;
    }

    /**
     * 修改wx群初步采集
     * 
     * @param sysWxBeforeGroup wx群初步采集
     * @return 结果
     */
    @Override
    public int updateSysWxBeforeGroup(SysWxBeforeGroup sysWxBeforeGroup)
    {
        sysWxBeforeGroup.setUpdateTime(DateUtils.getNowDate());

        if (sysWxBeforeGroup.getFlag()!=null&&!(sysWxBeforeGroup.getFlag().trim().isEmpty())&&sysWxBeforeGroup.getFlag().trim().equals("copy")){
            sysWxBeforeGroup.setId(null);
            //防重复
            sysWxBeforeGroup.setGroupNumber(sysWxBeforeGroup.getGroupNumber()+System.currentTimeMillis());
            sysWxBeforeGroup.setCreateTime(DateUtils.getNowDate());
            sysWxBeforeGroup.setMember(null);
            return sysWxBeforeGroupMapper.insertSysWxBeforeGroup(sysWxBeforeGroup);
        }else {
            //当群通过后  更新下时间，让在最前面
            if (sysWxBeforeGroup.getAttribute1().trim().equals("0")){
                //加群记录改为成功
                SysWxapplyRecord sysWxapplyRecord=new SysWxapplyRecord();
                sysWxapplyRecord.setGroupid(sysWxBeforeGroup.getId());
                List<SysWxapplyRecord>  list=  sysWxapplyRecordMapper.selectSysWxapplyRecordList(sysWxapplyRecord);
                if (list!=null&&list.size()>0){
                    SysWxapplyRecord newSys=list.get(0);
                    newSys.setAreamemo(sysWxBeforeGroup.getAreamemo());
                    newSys.setGroupid(sysWxBeforeGroup.getId());
                    newSys.setGroupNumber(sysWxBeforeGroup.getGroupNumber());
                    newSys.setGroupownerName(sysWxBeforeGroup.getGroupownerName());
                    newSys.setGroupownerPhone(sysWxBeforeGroup.getGroupownerPhone());
                    newSys.setJoinmemo(sysWxBeforeGroup.getJoinmemo());
                    newSys.setMember(sysWxBeforeGroup.getMember());
                    newSys.setSubBrand(sysWxBeforeGroup.getSubBrand());
                    newSys.setStatus("0");
                    sysWxapplyRecordMapper.updateSysWxapplyRecord(newSys);
                    sysWxBeforeGroup.setAttribute2(newSys.getMember());
                    sysWxBeforeGroup.setAttribute5(DateUtils.getNowDate());
                }else {//新插入一条
                    sysWxapplyRecord=new SysWxapplyRecord();
                    sysWxapplyRecord.setAreamemo(sysWxBeforeGroup.getAreamemo());
                    sysWxapplyRecord.setGroupid(sysWxBeforeGroup.getId());
                    sysWxapplyRecord.setGroupNumber(sysWxBeforeGroup.getGroupNumber());
                    sysWxapplyRecord.setGroupownerName(sysWxBeforeGroup.getGroupownerName());
                    sysWxapplyRecord.setGroupownerPhone(sysWxBeforeGroup.getGroupownerPhone());
                    sysWxapplyRecord.setJoinmemo(sysWxBeforeGroup.getJoinmemo());
                    sysWxapplyRecord.setMember(sysWxBeforeGroup.getMember());
                    sysWxapplyRecord.setSubBrand(sysWxBeforeGroup.getSubBrand());
                    sysWxapplyRecord.setStatus("0");
                    sysWxapplyRecordMapper.insertSysWxapplyRecord(sysWxapplyRecord);
                    sysWxBeforeGroup.setAttribute2(sysWxBeforeGroup.getMember());
                    sysWxBeforeGroup.setAttribute5(DateUtils.getNowDate());
                }

            }else  if (sysWxBeforeGroup.getMember()!=null&&!(sysWxBeforeGroup.getMember().trim().isEmpty())){
                SysWxapplyRecord sysWxapplyRecord=new SysWxapplyRecord();
                sysWxapplyRecord.setGroupid(sysWxBeforeGroup.getId());
                List<SysWxapplyRecord>  list=  sysWxapplyRecordMapper.selectSysWxapplyRecordList(sysWxapplyRecord);
                if (list!=null&&list.size()>0){
                    sysWxapplyRecord=list.get(0);
                    sysWxapplyRecord.setAreamemo(sysWxBeforeGroup.getAreamemo());
                    sysWxapplyRecord.setGroupid(sysWxBeforeGroup.getId());
                    sysWxapplyRecord.setGroupNumber(sysWxBeforeGroup.getGroupNumber());
                    sysWxapplyRecord.setGroupownerName(sysWxBeforeGroup.getGroupownerName());
                    sysWxapplyRecord.setGroupownerPhone(sysWxBeforeGroup.getGroupownerPhone());
                    sysWxapplyRecord.setJoinmemo(sysWxBeforeGroup.getJoinmemo());
                    sysWxapplyRecord.setMember(sysWxBeforeGroup.getMember());
                    sysWxapplyRecord.setSubBrand(sysWxBeforeGroup.getSubBrand());
                    sysWxapplyRecord.setStatus("1");
                    sysWxapplyRecordMapper.updateSysWxapplyRecord(sysWxapplyRecord);

                }else {//新插入一条
                    sysWxapplyRecord=new SysWxapplyRecord();
                    sysWxapplyRecord.setAreamemo(sysWxBeforeGroup.getAreamemo());
                    sysWxapplyRecord.setGroupid(sysWxBeforeGroup.getId());
                    sysWxapplyRecord.setGroupNumber(sysWxBeforeGroup.getGroupNumber());
                    sysWxapplyRecord.setGroupownerName(sysWxBeforeGroup.getGroupownerName());
                    sysWxapplyRecord.setGroupownerPhone(sysWxBeforeGroup.getGroupownerPhone());
                    sysWxapplyRecord.setJoinmemo(sysWxBeforeGroup.getJoinmemo());
                    sysWxapplyRecord.setMember(sysWxBeforeGroup.getMember());
                    sysWxapplyRecord.setSubBrand(sysWxBeforeGroup.getSubBrand());
                    sysWxapplyRecord.setStatus("1");
                    sysWxapplyRecordMapper.insertSysWxapplyRecord(sysWxapplyRecord);
                }
            }
            return sysWxBeforeGroupMapper.updateSysWxBeforeGroup(sysWxBeforeGroup);
        }


    }

    /**
     * 批量删除wx群初步采集
     * 
     * @param ids 需要删除的wx群初步采集主键
     * @return 结果
     */
    @Override
    public int deleteSysWxBeforeGroupByIds(Long[] ids)
    {
        if (ids!=null&&ids.length>0){
            for (int j=0;j<ids.length;j++){
                SysWxapplyRecord sysWxapplyRecord=new SysWxapplyRecord();
                sysWxapplyRecord.setGroupid(ids[j]);
                List<SysWxapplyRecord>  list=  sysWxapplyRecordMapper.selectSysWxapplyRecordList(sysWxapplyRecord);
                if (list!=null&&list.size()>0){
                    for (int i=0;i<list.size();i++){
                        SysWxapplyRecord inner=list.get(i);
                        inner.setStatus("-2");
                        sysWxapplyRecordMapper.updateSysWxapplyRecord(inner);
                    }
                }
            }
        }
        return sysWxBeforeGroupMapper.deleteSysWxBeforeGroupByIds(ids);
    }

    /**
     * 删除wx群初步采集信息
     * 
     * @param id wx群初步采集主键
     * @return 结果
     */
    @Override
    public int deleteSysWxBeforeGroupById(Long id)
    {
        SysWxapplyRecord sysWxapplyRecord=new SysWxapplyRecord();
        sysWxapplyRecord.setGroupid(id);
        List<SysWxapplyRecord>  list=  sysWxapplyRecordMapper.selectSysWxapplyRecordList(sysWxapplyRecord);
        if (list!=null&&list.size()>0){
            for (int i=0;i<list.size();i++){
                SysWxapplyRecord inner=list.get(i);
                inner.setStatus("-2");
                sysWxapplyRecordMapper.updateSysWxapplyRecord(inner);
            }
        }

        return sysWxBeforeGroupMapper.deleteSysWxBeforeGroupById(id);
    }
}
